package com.FH.applyForm.officeServies.FinanceBudget;

import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2016/10/14.
 */
@Stateless
public class FinanceBudgetsDAOImpl extends AccessDB implements FinanceBudgetDAO {

    /**
     * 查询财务预算多类别
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getFinanceBudgetInfo(String search, String page, String size) {

        ResultDTO resultDTO = new ResultDTO();
        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            String sqlexit = "select count(1) from financetype where 1=1 ";

            String sql_w = "";//查询条件sql组合

            String sql_page = "";//分页sql组合
            Integer page_ok = Integer.parseInt(page);
            Integer size_ok = Integer.parseInt(size);
            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok) + "," + size + "";
            }

            if (json.has("office_form_id")) {
                sql_w += " AND office_form_id = '" + json.getString("office_form_id") + "'";
            }
            if (json.has("office_id")) {
                sql_w += " AND office_id = '" + json.getString("office_id") + "'";
            }
            if (json.has("finance_budget_id")) {
                sql_w += " AND finance_budget_id = '" + json.getString("finance_budget_id") + "'";
            }
            String sql_ok = sqlexit + sql_w;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<FinanceBudgetShowDTO> result = new ArrayList<FinanceBudgetShowDTO>();
            if (!existName.equals("0")) {
                String sql = "select f.id,f.office_id,f.finance_budget_id,f.office_name,f.officetools_number,f.officetools_price,m.officetools_number from financetype f " +
                        "left join officetools_manage m on f.office_id = m.officetools_id where 1=1" + sql_w +  sql_page;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    FinanceBudgetShowDTO meetingroom = new FinanceBudgetShowDTO();
                    meetingroom.setId(seresult.get(i)[1]);
                    meetingroom.setOffice_id(seresult.get(i)[2]);
                    meetingroom.setOffice_form_id(seresult.get(i)[3]);
                    meetingroom.setOffice_name(seresult.get(i)[4]);
                    meetingroom.setOfficetools_number(seresult.get(i)[5]);
                    meetingroom.setOfficetools_price(seresult.get(i)[6]);
                    meetingroom.setOfficetools_total(seresult.get(i)[7]);

                    result.add(meetingroom);
                }
            }
            if (seresult.size() != 0) {
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }
            resultDTO.setStr(result.size() + "");
            resultDTO.setValues(result);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return resultDTO;

    }

    /**
     * 添加财务预算多类别
     * @param financeBudgetPullDTO
     * @return
     */
    @Override
    public ResultDTO addFinanceBudgetInfo(FinanceBudgetPullDTO financeBudgetPullDTO) {
        int resultls=0;
        ResultDTO resultDTO=new ResultDTO();

        String uuid= UUID.randomUUID().toString().trim().replaceAll("-", "");


        String Orgrole_insert="";
        Orgrole_insert="insert into financetype (id,office_id,finance_budget_id,office_name,officetools_number,officetools_price)" +
                "values('"+uuid+"','"+financeBudgetPullDTO.getOffice_id()+"','"+financeBudgetPullDTO.getFinance_budget_id()+"','"+financeBudgetPullDTO.getOffice_name()+"','"+financeBudgetPullDTO.getOfficetools_number()+"','"+financeBudgetPullDTO.getOfficetools_price()+"')";

        resultls=addReportingTo(Orgrole_insert);
        if(resultls!=0){
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.add_success);
        }else{
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.add_fail);
        }
        return resultDTO;
    }

    /**
     * 修改财务预算多类别
     * @param financeBudgetPullDTO
     * @return
     */
    @Override
    public ResultDTO updateFinanceBudgetInfo(FinanceBudgetPullDTO financeBudgetPullDTO) {
        ResultDTO resultDTO = new ResultDTO();

        String QUERY = "";

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //更新sql语句
        QUERY += "update officetype set ";
        if(null!=financeBudgetPullDTO.getOffice_id()&&!"".equals(financeBudgetPullDTO.getOffice_id())){
            QUERY += " office_id = '" + financeBudgetPullDTO.getOffice_id() + "'";
        }

        if(null!=financeBudgetPullDTO.getFinance_budget_id()&&!"".equals(financeBudgetPullDTO.getFinance_budget_id())){
            QUERY += ",office_form_id ='" + financeBudgetPullDTO.getFinance_budget_id() + "'";
        }

        if(null!=financeBudgetPullDTO.getOffice_name()&&!"".equals(financeBudgetPullDTO.getOffice_name())){
            QUERY += ",office_name ='" + financeBudgetPullDTO.getOffice_name() + "'";
        }

        if(null!=financeBudgetPullDTO.getOfficetools_number()&&!"".equals(financeBudgetPullDTO.getOfficetools_number())){
            QUERY += ",officetools_number ='" + financeBudgetPullDTO.getOfficetools_number() + "'";
        }

        if(null!=financeBudgetPullDTO.getOfficetools_price()&&!"".equals(financeBudgetPullDTO.getOfficetools_price())){
            QUERY += ",officetools_form_note ='" + financeBudgetPullDTO.getOfficetools_price() + "'";
        }

        QUERY += " where id = '" + financeBudgetPullDTO.getId() + "'";


        int result = this.addReportingTo(QUERY);

        // 判断SQl是否执行成功
        if (result != 0) {
            resultDTO.setStatus("90000");//
            resultDTO.setDescritpion(warnSrc.update_success);
        } else {
            resultDTO.setStatus("90001");//
            resultDTO.setDescritpion(warnSrc.update_fail);
        }
        return resultDTO;
    }

    /**
     * 删除财务预算多类别
     * @param financeBudgetPullDTO
     * @return
     */
    @Override
    public ResultDTO deleteFinanceBudgetInfo(FinanceBudgetPullDTO financeBudgetPullDTO) {
        String sql="select count(1) from financetype WHERE finance_budget_id='"+financeBudgetPullDTO.getFinance_budget_id()+"'";
        String existadvance= getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();
        // 删除SQL语句
        if(!existadvance.equals("0")){
            String QUERY = "";
            QUERY += "delete from financetype  where finance_budget_id='"+financeBudgetPullDTO.getFinance_budget_id()+"'";

            result = this.addReportingTo(QUERY);
        }

        // 判断SQl是否执行成功
        if (result!= 0) {

            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);
        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);
        }
        return resultDTO;
    }
}
